<template>
  <div>
    <table-ext :columns="columns" :searchForm="searchForm" :config="config" ref="tableExt" @checkedData="getCheckedData">
        <template slot="table-action">
          <el-button  type="primary" icon="el-icon-plus" v-if="this.dlvFlowId != '106303'" @click="turnNpcTask()">转物料认证</el-button>
          <el-button  type="primary" icon="el-icon-plus" v-if="this.dlvFlowId == '106303'" @click="turnFactoryInspect()">转考察评估</el-button> <!-- 106303:现场考察节点ID -->
        </template>
    </table-ext>
  </div>
</template>
<script>
  import TableExt from '@/components/common/table-ext/table-ext.vue';
  import TableIcon from '@/components/common/table-ext/table-icon';
  import SelectTree from '@/components/common/select-ext/select-tree.vue';
  export default {
    // 引入自定义表格组件
    components:{
      'table-ext': TableExt,
      SelectTree
    },
    data() {
      return {
        dlvFlowId:'', // 开发流程节点ID
        checkedData: [], // DataGrid选中的数据
        config:{ // 页面共通配置
          title: '开发申请流程待办', //页面标题
          isPage: true, //是否分页
          isNo: true, //是否显示序号
          selection: true, //是否显示复选框
          crudMapping:{ // CRUD方法
            query:{
              url: this.lib.config.host.vendorDlv + "/vendormgt/dlv/dlvApply/queryFlowNode",
              data:{}
            }
          }
        },
        // 查询表单配置
        searchForm:[{
          label: '采购组织:',
          key: 'purOrgId',
          type: 'select',
          dict: {
            url: this.lib.config.host.masterData + '/system/purOrgMgt/purOrg/getPurOrgByUserId/' + this.getCurrUserId(),
            method: 'get',
            label: 'purOrgName',
            value: 'id'
          }
        },{
          label: '采购组:',
          key: 'purGroupId',
          type: 'select',
          dict: {
            url: this.lib.config.host.masterData + '/mdata/purOrgMgt/purGroup/getByPurOrgId/{purOrgId}',
            method: 'post',
            label: 'purGroupName',
            value: 'id'
          }
        },{
          label: '品类:',
          key: 'categoryCode',
          type: 'select-tree'
        }],
        // 表格列定义
        columns:[{
          label: '采购组织',
          key: 'purOrgName',
          align: 'center'
        },{
          label: '采购组',
          key: 'purGroupName',
          align: 'center'
        },{
          label: '申请号',
          key: 'applyNo',
          align:'center',
        },{
          label: '申请主题',
          key: 'title',
          width:'200',
          align:'center',
        },{
          label: '品类编码',
          key: 'categoryCode',
          align: 'center'
        },{
          label: '品类名称',
          key: 'categoryName',
          align: 'center',
          width:'200px'
        },{
          label: '供应商ID',
          key: 'vendorId',
          align: 'center',
          hidden:true
        },{
          label: '供应商编码',
          key: 'vendorCode',
          align: 'center'
        },{
          label: '供应商名称',
          key: 'vendorName',
          align: 'center',
          width:'200px'
        },{
          label: '开发申请明细ID',
          key: 'applyItemId',
          align: 'center',
          hidden:true
        },{
          label: '流程名称',
          key: 'dlvFlowName',
          align: 'center'
        }]
      };
    },
    created(){
      if(this.$route.query){
        this.config.crudMapping.query.data = this.$route.query;
        this.dlvFlowId = this.$route.query.dlvFlowId;
      }
    },
    mounted() {

    },
    methods: {
      // 获取选中行的数据（子组件传递）
      getCheckedData(data){
        this.checkedData = data;
      },
      // 转物料认证页面
      turnNpcTask() {
        let validateResult = this.turnBeforValidateResult();
        if(validateResult){ // 验证通过方可执行
          this.$confirm('确认转物料认证嘛？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$router.push({path: '/Index/VendorDlv/Npc/Buyer/Withmtrl/Add/Tab', query:{'checkedData': this.checkedData,'dlvFlowId': this.dlvFlowId}})
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消操作!'
            });
          });
        }
      },
      // 转工厂考察评估页面
      turnFactoryInspect() {
        let validateResult = this.turnBeforValidateResult();
        if(validateResult){ // 验证通过方可执行
          this.$confirm('确认转工厂考察评估嘛？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$router.push({path: '/Index/VendorDlv/VendorInspectAdd', query:{'checkedData': this.checkedData}})
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消操作!'
            });
          });
        }
      },
      // 待办跳转前数据可行性验证
      turnBeforValidateResult() {
        let validateResult = true;
        if(this.checkedData.length <= 0){
          this.$message({
            type: 'warning',
            message: '请选择数据!'
          });
          return validateResult = false;
        } else {
          const purOrgId = this.checkedData[0].purOrgId;
          const purGroupId = this.checkedData[0].purGroupId;
          const categoryCode = this.checkedData[0].categoryCode;
          this.checkedData.forEach(element =>{
            // 数据采购组织需保持一致
            if(purOrgId != element.purOrgId){
              this.$message({
                type: 'warning',
                message: '采购组织选择不一致，请筛选数据!'
              });
              return validateResult = false;
            } 
            // 数据采购组需保持一致
            if(purGroupId != element.purGroupId){
              this.$message({
                type: 'warning',
                message: '采购组选择不一致，请筛选数据!'
              });
              return validateResult = false;
            }
            // 数据品类需保持一致
            if(categoryCode != element.categoryCode){
              this.$message({
                type: 'warning',
                message: '品类选择不一致，请筛选数据!'
              });
              return validateResult = false;
            }  
          })
        }
        return validateResult;
      }
    }
  }
</script>